/**
 * 
 */
package neptune.manager.validator.rules.topology;

import neptune.manager.model.topology.IPAddress;
import neptune.manager.model.topology.NetworkInterface;
import neptune.manager.model.topology.Node;
import neptune.manager.model.topology.Topology;
import neptune.manager.utils.Logger.MessageType;
import neptune.manager.validator.ValidatorResult;
import neptune.manager.validator.ValidatorRule;
import neptune.manager.validator.Validator.Status;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class ExperimentIPAddressRangeRule extends ValidatorRule<Topology> {

	private IPAddress expSubnet;

	public ExperimentIPAddressRangeRule(IPAddress expSubnet) {
		super();
		this.expSubnet = expSubnet;
	}

	@Override
	public String getName() {
		return "ExperimentIPAddressRangeRule";
	}

	@Override
	protected ValidatorResult validationAlgorithm(Topology toValidate,
			ValidatorResult result) {

		result.getLog().addMessage("Verifying IP Address range",
				MessageType.NORMAL);

		for (Node node : toValidate.getNodes()) {
			result.getLog().addMessage("Node " + node.getId(),
					MessageType.NORMAL);
			for (NetworkInterface netIf : node.getInterfaces()) {
				result.getLog().addMessage(
						"Interface " + netIf.getInterfaceName(),
						MessageType.NORMAL);
				for (IPAddress ipAddr : netIf.getIpAddresses()) {
					if (ipAddr.isControlAddress())
						continue; // I control address non sono inclusi nel
					// range

					result.getLog().addMessage("IP address " + ipAddr,
							MessageType.NORMAL);
					if (!expSubnet.include(ipAddr)) {
						result.setStatus(Status.ERROR);
						result
								.getLog()
								.addMessage(
										"IP address '"
												+ ipAddr.getAddress()
												+ "' is not in the experiment Ip address range",
										MessageType.ERROR);
					}
				}
			}
		}

		return result;
	}

}
